package com.business_clean.app.weight.dialog;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.config.AsyncRequestUtil;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.DividerItemDecoration;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.app.weight.CustomAvatarView;
import com.business_clean.data.mode.project.ProjectManager;
import com.business_clean.ui.activity.custom.CustomManagerActivity;
import com.business_clean.ui.activity.project.ProjectManagerActivity;
import com.business_clean.ui.adapter.custom.CustomMangerAllAdapter;
import com.business_clean.ui.adapter.custom.CustomMangerNoPaddingAllAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.core.DrawerPopupView;
import com.zhixinhuixue.library.common.ext.DensityExtKt;


/**
 * 全局 选择 项目的内容
 */
public class PagerDrawerPopup extends BottomPopupView {

    private RecyclerView recyclerView;
    private Context mContext;

    private CustomMangerNoPaddingAllAdapter mAdapter;

    public PagerDrawerPopup(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.home_drawer;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = findViewById(R.id.recyclerview_home_drawer);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CustomMangerNoPaddingAllAdapter();
        recyclerView.addItemDecoration(new DividerItemDecoration(mContext));
        recyclerView.setAdapter(mAdapter);

        //反选
        if (App.appViewModelInstance.getAllProjectManager().getValue() != null) {
            mAdapter.setList(App.appViewModelInstance.getAllProjectManager().getValue().getList());
            mAdapter.updateChoose(App.appViewModelInstance.getUserInfo().getValue().getProject().getUuid());
        }


        //监听项目
        App.appViewModelInstance.getAllProjectManager().observe(this, new Observer<ProjectManager>() {
            @Override
            public void onChanged(ProjectManager projectManager) {
                mAdapter.setList(projectManager.getList());
                mAdapter.updateChoose(App.appViewModelInstance.getUserInfo().getValue().getProject().getUuid());
            }
        });

        if (mAdapter != null) {
            mAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    ToastUtil.show("成功切换为" + mAdapter.getData().get(position).getProject_name());
                    mAdapter.updateChoose(mAdapter.getData().get(position).getUuid());
                    App.appViewModelInstance.getUserInfo().getValue().setProject(mAdapter.getData().get(position));
                    App.appViewModelInstance.getProjectInfo().setValue(mAdapter.getData().get(position));
                    AsyncRequestUtil.requestChangeProject(mAdapter.getData().get(position).getUuid());
                    AsyncRequestUtil.requestUserInfoData();
                    dialog.dismiss();
                }
            });

            mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                    if (view.getId() == R.id.ck_item_choose) {
                        ToastUtil.show("成功切换为" + mAdapter.getData().get(position).getProject_name());
                        mAdapter.updateChoose(mAdapter.getData().get(position).getUuid());
                        App.appViewModelInstance.getUserInfo().getValue().setProject(mAdapter.getData().get(position));
                        App.appViewModelInstance.getProjectInfo().setValue(mAdapter.getData().get(position));
                        AsyncRequestUtil.requestChangeProject(mAdapter.getData().get(position).getUuid());
                        AsyncRequestUtil.requestUserInfoData();
                        dialog.dismiss();
                    }
                }
            });
        }


        findViewById(R.id.tv_dialog_base_bottom_cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    protected int getMaxHeight() {
        return (int) (DensityExtKt.getScreenHeight() / 1.5);
    }


    @Override
    protected void onShow() {
        super.onShow();
        Log.e("tag", "PagerDrawerPopup onShow");
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        Log.e("tag", "PagerDrawerPopup onDismiss");
    }

}

